﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AForge.Robotics.Lego;

namespace nxt
{
    class NxtModul
    {
        NXTBrick m_robot = new NXTBrick();
        NXTBrick.MotorState motorState = new NXTBrick.MotorState();
        private string comPort;

        public NxtModul(string _comPort)
        {
            comPort = _comPort;
        }
                
        public void Connect()
        {
            m_robot.Connect(comPort);
        }

        public bool nxtConnectionStatus()
        {
            bool status = false;

            return status = m_robot.IsConnected;
        }

        public void Disconnect()
        {

            try
            {
                m_robot.Disconnect();
            }
            catch
            {
                //
            }
        }

        public void startMotorA()
        {
            try
            {

                motorState.Power = 20;
                motorState.TurnRatio = 50;
                motorState.Mode = NXTBrick.MotorMode.On;
                motorState.Regulation = NXTBrick.MotorRegulationMode.Idle;
                motorState.RunState = NXTBrick.MotorRunState.Running;
                motorState.TachoLimit = 100;

                m_robot.SetMotorState(NXTBrick.Motor.A, motorState);

                /*if (nxt.GetSensorValue(NXTBrick.Sensor.First, out sensorValues))
                {
                    // ...
                }*/
            }
            catch
            {
                //
            }
        }

        public void startMotorB()
        {
            try
            {

                motorState.Power = 20;
                motorState.TurnRatio = 50;
                motorState.Mode = NXTBrick.MotorMode.On;
                motorState.Regulation = NXTBrick.MotorRegulationMode.Idle;
                motorState.RunState = NXTBrick.MotorRunState.Running;
                motorState.TachoLimit = 100;

                m_robot.SetMotorState(NXTBrick.Motor.B, motorState);
            }
            catch
            {

            }
        }

        public void startMotorC()
        {
            try
            {

                motorState.Power = 20;
                motorState.TurnRatio = 50;
                motorState.Mode = NXTBrick.MotorMode.On;
                motorState.Regulation = NXTBrick.MotorRegulationMode.Idle;
                motorState.RunState = NXTBrick.MotorRunState.Running;
                motorState.TachoLimit = 100;

                m_robot.SetMotorState(NXTBrick.Motor.C, motorState);
            }
            catch
            {
                //
            }
        }

        public int BatteryInfo()
        {
            int batteryPower;
            m_robot.GetBatteryPower(out batteryPower);
            return batteryPower;
        }

        public string DeviceInfo()
        {
            string deviceName;
            byte [] btaddress;
            int btsignal;
            int fUserSignal;

            m_robot.GetDeviceInformation(out deviceName, out btaddress, out btsignal, out fUserSignal);

            return (deviceName + " " + Convert.ToString(btaddress) + " " + Convert.ToString(btsignal) + " " + Convert.ToString(fUserSignal));
        }

     }
}
